package com.water.wfo.user.ability.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.water.wfo.sys.pojo.entity.UserEntity;
import com.water.wfo.sys.pojo.request.UserQueryRequest;
import com.water.wfo.sys.pojo.response.UserResponse;
import com.water.wfo.user.ability.UserAbility;
import com.water.wfo.user.mapper.UserInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * packageName com.water.wfo.user.ability.impl
 *
 * @author junhuili
 * @version JDK 8
 * @date 2024/5/20
 * @description TODO
 */
@Slf4j
@Component
public class UserAbilityImpl implements UserAbility {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public int insert(UserEntity entity) {
        entity.setActive("Y");
        return userInfoMapper.insert(entity);
    }

    @Override
    public int insertList(List<UserEntity> list) {
        return userInfoMapper.insertList(list);
    }

    @Override
    public int deleteByloginId(Long loginId) {
        return userInfoMapper.deleteById(loginId);
    }

    @Override
    public void update(UserEntity user) {
        userInfoMapper.update(user);
    }

    @Override
    public List<UserEntity> queryAll(UserEntity user) {
        return userInfoMapper.queryAll(user);
    }

    @Override
    public UserEntity selectUserById(String loginId) {
        return userInfoMapper.selectUserById(loginId);
    }

    @Override
    public UserResponse queryUserListInfo(UserQueryRequest request) {
        List<UserEntity> userEntities = null;
        try {
            PageHelper.startPage(request.getPage(),request.getSize());
            userEntities = userInfoMapper.queryUserListInfo(request);
        } catch (Exception e) {
           log.error(e.getMessage());
        } finally {
            PageHelper.clearPage();
        }
        //得到分页的结果对象
        PageInfo<UserEntity> personPageInfo = new PageInfo<>(userEntities);
        //得到分页中的person条目对象
        List<UserEntity> pageList = personPageInfo.getList();
        UserResponse response = new UserResponse();
        response.setUsers(pageList);
        response.setTotal(personPageInfo.getTotal());
        return response;
    }

    @Override
    public int deleteBatchByIds(List<Long> ids) {
        return userInfoMapper.deleteBatchByIds(ids);
    }
}
